package com.bubblegumproject.ogo.swing.binding;

import java.util.*;

/**
 * Basic implementation of the {@link BoundedFormatter} that automates much of the logic.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class BasicBoundedFormatter<D, S> implements BoundedFormatter<D, S> {

    protected LinkedHashMap<D, S> valueMap = new LinkedHashMap<D, S>();

    public List<S> getPrintValues() {
        return new ArrayList<S>(valueMap.values());
    }

    public S format(D domainValue) {
        return valueMap.get(domainValue);
    }

    public D parse(S stringValue) {
        for (Map.Entry<D, S> entry : valueMap.entrySet()) {
            if (entry.getValue().equals(stringValue)) return entry.getKey();
        }
        throw new Error(this + " can't parse StringValue " + stringValue);
    }
}
